package rad.framework.uml2.uml.edit.providers.elementItemProvider.namedElement.nameSpace.state;

import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.ViewerNotification;
import org.eclipse.uml2.uml.State;
import org.eclipse.uml2.uml.UMLPackage;

import rad.framework.uml2.uml.edit.UMLEditPlugin;
import rad.framework.uml2.uml.edit.providers.elementItemProvider.namedElement.nameSpace.NamespaceItemProvider;

public class StateItemProvider
		extends NamespaceItemProvider
		implements IEditingDomainItemProvider, IStructuredItemContentProvider, ITreeItemContentProvider, IItemLabelProvider, IItemPropertySource {

	public StateItemProvider(AdapterFactory adapterFactory) {
		super(adapterFactory);
	}

	@Override
	public List getPropertyDescriptors(Object object) {
		if (itemPropertyDescriptors == null) {
			super.getPropertyDescriptors(object);

			addIsLeafPropertyDescriptor(object);
			addRedefinedElementPropertyDescriptor(object);
			addRedefinitionContextPropertyDescriptor(object);
			addOutgoingPropertyDescriptor(object);
			addIncomingPropertyDescriptor(object);
			addContainerPropertyDescriptor(object);
			addIsCompositePropertyDescriptor(object);
			addIsOrthogonalPropertyDescriptor(object);
			addIsSimplePropertyDescriptor(object);
			addIsSubmachineStatePropertyDescriptor(object);
			addSubmachinePropertyDescriptor(object);
			addConnectionPropertyDescriptor(object);
			addConnectionPointPropertyDescriptor(object);
			addRedefinedStatePropertyDescriptor(object);
			addStateInvariantPropertyDescriptor(object);
			addEntryPropertyDescriptor(object);
			addExitPropertyDescriptor(object);
			addDoActivityPropertyDescriptor(object);
			addDeferrableTriggerPropertyDescriptor(object);
			addRegionPropertyDescriptor(object);
		}
		return itemPropertyDescriptors;
	}

	protected void addIsLeafPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_RedefinableElement_isLeaf_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_RedefinableElement_isLeaf_feature", "_UI_RedefinableElement_type"),
				UMLPackage.Literals.REDEFINABLE_ELEMENT__IS_LEAF, true, ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE, null, null));
	}

	protected void addRedefinedElementPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_RedefinableElement_redefinedElement_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_RedefinableElement_redefinedElement_feature", "_UI_RedefinableElement_type"),
				UMLPackage.Literals.REDEFINABLE_ELEMENT__REDEFINED_ELEMENT, false, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addRedefinitionContextPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_RedefinableElement_redefinitionContext_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_RedefinableElement_redefinitionContext_feature", "_UI_RedefinableElement_type"),
				UMLPackage.Literals.REDEFINABLE_ELEMENT__REDEFINITION_CONTEXT, false, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addOutgoingPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Vertex_outgoing_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Vertex_outgoing_feature", "_UI_Vertex_type"),
				UMLPackage.Literals.VERTEX__OUTGOING, true, null, null, null));
	}

	protected void addIncomingPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Vertex_incoming_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Vertex_incoming_feature", "_UI_Vertex_type"),
				UMLPackage.Literals.VERTEX__INCOMING, true, null, null, null));
	}

	protected void addContainerPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_Vertex_container_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_Vertex_container_feature", "_UI_Vertex_type"),
				UMLPackage.Literals.VERTEX__CONTAINER, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addIsCompositePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_State_isComposite_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_State_isComposite_feature", "_UI_State_type"),
				UMLPackage.Literals.STATE__IS_COMPOSITE, false, ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addIsOrthogonalPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_State_isOrthogonal_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_State_isOrthogonal_feature", "_UI_State_type"),
				UMLPackage.Literals.STATE__IS_ORTHOGONAL, false, ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addIsSimplePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_State_isSimple_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_State_isSimple_feature", "_UI_State_type"),
				UMLPackage.Literals.STATE__IS_SIMPLE, false, ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addIsSubmachineStatePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_State_isSubmachineState_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_State_isSubmachineState_feature", "_UI_State_type"),
				UMLPackage.Literals.STATE__IS_SUBMACHINE_STATE, false, ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addSubmachinePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_State_submachine_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_State_submachine_feature", "_UI_State_type"),
				UMLPackage.Literals.STATE__SUBMACHINE, true, null, null, null));
	}

	protected void addConnectionPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_State_connection_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_State_connection_feature", "_UI_State_type"),
				UMLPackage.Literals.STATE__CONNECTION, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addConnectionPointPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_State_connectionPoint_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_State_connectionPoint_feature", "_UI_State_type"),
				UMLPackage.Literals.STATE__CONNECTION_POINT, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addRedefinedStatePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_State_redefinedState_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_State_redefinedState_feature", "_UI_State_type"),
				UMLPackage.Literals.STATE__REDEFINED_STATE, true, null, null, null));
	}

	protected void addStateInvariantPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_State_stateInvariant_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_State_stateInvariant_feature", "_UI_State_type"),
				UMLPackage.Literals.STATE__STATE_INVARIANT, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addEntryPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_State_entry_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_State_entry_feature", "_UI_State_type"),
				UMLPackage.Literals.STATE__ENTRY, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addExitPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_State_exit_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_State_exit_feature", "_UI_State_type"),
				UMLPackage.Literals.STATE__EXIT, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addDoActivityPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_State_doActivity_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_State_doActivity_feature", "_UI_State_type"),
				UMLPackage.Literals.STATE__DO_ACTIVITY, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addDeferrableTriggerPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_State_deferrableTrigger_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_State_deferrableTrigger_feature", "_UI_State_type"),
				UMLPackage.Literals.STATE__DEFERRABLE_TRIGGER, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addRegionPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_State_region_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_State_region_feature", "_UI_State_type"),
				UMLPackage.Literals.STATE__REGION, true, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	@Override
	public Collection getChildrenFeatures(Object object) {
		if (childrenFeatures == null) {
			super.getChildrenFeatures(object);
			childrenFeatures.add(UMLPackage.Literals.STATE__CONNECTION);
			childrenFeatures.add(UMLPackage.Literals.STATE__CONNECTION_POINT);
			childrenFeatures.add(UMLPackage.Literals.STATE__STATE_INVARIANT);
			childrenFeatures.add(UMLPackage.Literals.STATE__ENTRY);
			childrenFeatures.add(UMLPackage.Literals.STATE__EXIT);
			childrenFeatures.add(UMLPackage.Literals.STATE__DO_ACTIVITY);
			childrenFeatures.add(UMLPackage.Literals.STATE__DEFERRABLE_TRIGGER);
			childrenFeatures.add(UMLPackage.Literals.STATE__REGION);
		}
		return childrenFeatures;
	}

	protected EStructuralFeature getChildFeature(Object object, Object child) {

		return super.getChildFeature(object, child);
	}

	public Object getImage(Object object) {
		return overlayImage(object, getResourceLocator().getImage("full/obj16/State"));
	}

	public String getText(Object object) {
		return appendLabel(appendType(appendKeywords(new StringBuffer(), object), "_UI_State_type"), object).toString();

	}

	public void notifyChanged(Notification notification) {
		updateChildren(notification);

		switch (notification.getFeatureID(State.class)) {
			case UMLPackage.STATE__IS_LEAF:
			case UMLPackage.STATE__IS_COMPOSITE:
			case UMLPackage.STATE__IS_ORTHOGONAL:
			case UMLPackage.STATE__IS_SIMPLE:
			case UMLPackage.STATE__IS_SUBMACHINE_STATE:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true));
				return;
			case UMLPackage.STATE__CONNECTION:
			case UMLPackage.STATE__CONNECTION_POINT:
			case UMLPackage.STATE__STATE_INVARIANT:
			case UMLPackage.STATE__ENTRY:
			case UMLPackage.STATE__EXIT:
			case UMLPackage.STATE__DO_ACTIVITY:
			case UMLPackage.STATE__DEFERRABLE_TRIGGER:
			case UMLPackage.STATE__REGION:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
				return;
		}
		super.notifyChanged(notification);
	}

	protected void collectNewChildDescriptors(Collection newChildDescriptors, Object object) {
		super.collectNewChildDescriptors(newChildDescriptors, object);

	}

	public String getCreateChildText(Object owner, Object feature, Object child, Collection selection) {
		final Object childFeature = feature;
		final Object childObject = child;

		final boolean qualify = childFeature == UMLPackage.Literals.NAMESPACE__OWNED_RULE || childFeature == UMLPackage.Literals.STATE__STATE_INVARIANT || childFeature == UMLPackage.Literals.STATE__ENTRY || childFeature == UMLPackage.Literals.STATE__EXIT
				|| childFeature == UMLPackage.Literals.STATE__DO_ACTIVITY;

		if (qualify) {
			return getString("_UI_CreateChild_text2",
					new Object[] { getTypeText(childObject), getFeatureText(childFeature), getTypeText(owner) });
		}
		return super.getCreateChildText(owner, feature, child, selection);
	}

	public ResourceLocator getResourceLocator() {
		return UMLEditPlugin.INSTANCE;
	}

}
